package com.ruoyi.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.ApprovalR;
import com.ruoyi.mapper.ApprovalRecordsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.ErpSupplierManagementMapper;
import com.ruoyi.domain.ErpSupplierManagement;
import com.ruoyi.service.IErpSupplierManagementService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 供应商管理Service业务层处理
 * 
 * @author erp
 * @date 2025-07-15
 */
@Service
@Transactional
public class ErpSupplierManagementServiceImpl extends ServiceImpl<ErpSupplierManagementMapper,ErpSupplierManagement> implements IErpSupplierManagementService  {
    @Autowired
    private ErpSupplierManagementMapper erpSupplierManagementMapper;
    @Autowired
    private ApprovalRecordsMapper approvalRecordMapper;

    /**
     * 查询供应商管理
     *
     * @param id 供应商管理主键
     * @return 供应商管理
     */
    @Override
    public ErpSupplierManagement selectErpSupplierManagementById(String id) {
        return erpSupplierManagementMapper.selectErpSupplierManagementById(id);
    }

    /**
     * 查询供应商管理列表
     *
     * @param erpSupplierManagement 供应商管理
     * @return 供应商管理
     */
    @Override
    public List<ErpSupplierManagement> selectErpSupplierManagementList(ErpSupplierManagement erpSupplierManagement) {
        return erpSupplierManagementMapper.selectErpSupplierManagementList(erpSupplierManagement);
    }

    /**
     * 新增供应商管理
     *
     * @param erpSupplierManagement 供应商管理
     * @return 结果
     */
    @Override
    public int insertErpSupplierManagement(ErpSupplierManagement erpSupplierManagement) {
        erpSupplierManagement.setCreateTime(DateUtils.getNowDate());
        return erpSupplierManagementMapper.insertErpSupplierManagement(erpSupplierManagement);
    }

    /**
     * 修改供应商管理
     *
     * @param erpSupplierManagement 供应商管理
     * @return 结果
     */
    @Override
    public int updateErpSupplierManagement(ErpSupplierManagement erpSupplierManagement) {
        erpSupplierManagement.setUpdateTime(DateUtils.getNowDate());
        return erpSupplierManagementMapper.updateErpSupplierManagement(erpSupplierManagement);
    }

    /**
     * 批量删除供应商管理
     *
     * @param ids 需要删除的供应商管理主键
     * @return 结果
     */
    @Override
    public int deleteErpSupplierManagementByIds(String[] ids) {
        return erpSupplierManagementMapper.deleteErpSupplierManagementByIds(ids);
    }

    /**
     * 删除供应商管理信息
     *
     * @param id 供应商管理主键
     * @return 结果
     */
    @Override
    public int deleteErpSupplierManagementById(String id) {
        return erpSupplierManagementMapper.deleteErpSupplierManagementById(id);
    }

    @Override
    public Map<String, Integer> updateAudit(ErpSupplierManagement erpSupplierManagement) {
        LambdaUpdateWrapper<ErpSupplierManagement> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(ErpSupplierManagement::getId, erpSupplierManagement.getId())
                .set(ErpSupplierManagement::getOpinion,erpSupplierManagement.getOpinion()).set(ErpSupplierManagement::getChoice,erpSupplierManagement.getChoice());
        int update = erpSupplierManagementMapper.update(null, lambdaUpdateWrapper);
        System.out.println("erpSupplierManagement.getChoice() = " + erpSupplierManagement.getChoice());

        Map<String, Integer> auditMap = new HashMap<>();
        lambdaUpdateWrapper.clear();


        if (erpSupplierManagement.getChoice() == 0) {
            lambdaUpdateWrapper.eq(ErpSupplierManagement::getId, erpSupplierManagement.getId())
                    .set(ErpSupplierManagement::getAudit, 3);
            erpSupplierManagementMapper.update(null, lambdaUpdateWrapper);
        } else if (erpSupplierManagement.getChoice() == 1) {
            lambdaUpdateWrapper.eq(ErpSupplierManagement::getId, erpSupplierManagement.getId())
                    .set(ErpSupplierManagement::getAudit, 2);
            erpSupplierManagementMapper.update(null, lambdaUpdateWrapper);
        } else {
            throw new IllegalArgumentException("Invalid choice: " + erpSupplierManagement.getChoice());
        }
        if (update > 0) {
            switch (erpSupplierManagement.getChoice()) {
            //0 代表审核按钮为使用状态 1代表审核按钮为禁用状态
                case 0 ->  auditMap.put("审核状态", 0);
                case 1 ->  auditMap.put("审核状态", 1);
                default->   auditMap.put("审核状态", 2);
            };
            //审核状态0为已经审核
            auditMap.put("审核状态", 0);
            auditMap.put("提交成功？", 1);
            //添加审核记录 1已审核
            System.out.println("erpSupplierManagement.getId() = " + erpSupplierManagement.getId());
           // 获取记录
            ErpSupplierManagement one = getOne(
                    new LambdaQueryWrapper<ErpSupplierManagement>()
                            .eq(ErpSupplierManagement::getId, erpSupplierManagement.getId()));
            //判断如果one获取到了就 加入到审核记录表
            if (!StringUtils.isBlank(one.getTransactionCurrency())) {
                ApprovalR approvalR1 = new ApprovalR();
                approvalR1.setBusinessId(erpSupplierManagement.getAudit());
                approvalR1.setBusinessType("供应商管理");
                // 格式化输出
                LocalDateTime now = LocalDateTime.now();
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                String formattedTime = now.format(formatter);
                System.out.println("格式化后：" + formattedTime); // 输出：2025-07-21 15:30:00
                approvalR1.setApprovalTime(formattedTime);

               approvalR1.setCreateTime(one.getUpdateTime());
                approvalR1.setUpdateTime(one.getUpdateTime());
                approvalR1.setOpinion(one.getOpinion());
                approvalR1.setResult(one.getAudit());
                approvalR1.setJiId(one.getId());
                int insert = approvalRecordMapper.insert(approvalR1);
                System.err.println(insert);
            }
            return auditMap;
        } else {
            auditMap.put("审核失败", 0);
            return auditMap;
        }
        // 1. 传入 null 代表不需要依赖于实体对象

    }




    @Override
    public int rejectaudit(int id) {
        LambdaUpdateWrapper<ErpSupplierManagement> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(ErpSupplierManagement::getId, id).set(ErpSupplierManagement::getAudit, 1);
        int update = erpSupplierManagementMapper.update(null, lambdaUpdateWrapper);
        if (update>0){
            return update;
        }
          return 0;
    }


    @Override
    public Map<String, Integer> submit(int id) {
        LambdaUpdateWrapper<ErpSupplierManagement> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(ErpSupplierManagement::getId, id).set(ErpSupplierManagement::getAudit, 1);
        int update = erpSupplierManagementMapper.update(null, lambdaUpdateWrapper);
        //可以进行审核
        Map<String, Integer> objectObjectMap = new HashMap<>();
        if (update > 0) {

            //审核状态为可审核
            objectObjectMap.put("审核状态", 1);
            objectObjectMap.put("提交成功？", 1);
            return objectObjectMap;
        } else {
            objectObjectMap.put("提交失败", 0);
            return objectObjectMap;
        }


    }

    @Override
    public List<ErpSupplierManagement> getsupper() {

        LambdaQueryWrapper<ErpSupplierManagement> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(ErpSupplierManagement::getAudit,2);
        List<ErpSupplierManagement> erpSupplierManagements = erpSupplierManagementMapper.selectList(lambdaQueryWrapper);
        return erpSupplierManagements;
    }


}
